<template>
  <div class="dashboard-container">
    <div class="app-container">
        <el-card>
          <el-tabs>
            <!-- 角色管理 -->
            <el-tab-pane label="角色管理">
              <!-- 新增角色按钮 -->
              <el-row style="height:60px">
                <el-button
                  icon="el-icon-plus"
                  size="small"
                  type="primary"
                  @click="showDialog = true"
                >新增角色</el-button>
              </el-row>
                <!-- table表格 -->
                <el-table :data='list' border=''>
                  <el-table-column label="序号" width="120" type='index' align="center"/>
                  <el-table-column label="角色名称" width="240" prop="name" align="center"/>
                  <el-table-column label="描述" prop='description' align="center"/>
                  <el-table-column label="操作" align="center">
                    <template slot-scope="{row}">
                      <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                      <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                      <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
                <!-- 分页组件 -->
                <el-row type="flex" justify="center" align="middle" style="height: 60px">
                  <el-pagination layout="prev, pager, next" :total='page.total' :page-size='page.pagesize' :current-page='page.page' @current-change='changePage'/>
                </el-row>
            </el-tab-pane>
             <!-- /角色管理 -->


            <!-- 公司信息 -->
            <el-tab-pane label="公司信息">
              <!-- 提示信息 -->
               <el-alert show-icon title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改" type="info"  :closable="false"> </el-alert>
              <!-- form表单 -->
              <el-form label-width="120px" style="margin-top:50px">
                <el-form-item label="公司名称">
                  <el-input v-model="formData.name" disabled style="width:400px" />
                </el-form-item>
                <el-form-item label="公司地址">
                  <el-input v-model="formData.companyAddress" disabled style="width:400px" />
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input v-model="formData.mailbox" disabled style="width:400px" />
                </el-form-item>
                <el-form-item label="备注">
                  <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </el-card>
    </div>
    <!-- 弹出层 -->
    <el-dialog  :title="roleForm.id ? '编辑' : '新增'" :visible="showDialog" @close="btnCancel">
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限弹出层 -->
    <el-dialog title="" :visible='showPermDialog' @close="btnPermCancel">
        <!-- 权限是一棵树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- default-expand-all默认展开 -->
        <!-- show-checkbox展示复选框 -->
        <!-- check-strictly 如果为true 那表示父子勾选时 不互相关联 如果为false就互相关联 -->
        <!-- :node-key='id' id作为唯一标识 -->
        <el-tree 
        :data='permData' 
        :default-checked-keys='selectCheck' 
        :show-checkbox='true' 
        node-key="id" 
        :check-strictly='true' 
        :default-expand-all='true'  
        :props='defaultProps'
        ref="permTree"
        />
        <!-- 确定取消 -->
       <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOk">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import {getRoleList, getCompanyInfo , deleteRole , getRoleDetail,updateRole,addRole,assignPerm} from '@/api/setting'
import {getPermissionList} from '@/api/permission'
import {transListToTreeData} from '@/utils' 
export default {
  data () {
    return {
      list:[],
      permData:[], //接收权限数据
      selectCheck:[], //用来记录当前的权限点的标识
      defaultProps:{
        label:'name'
      }, // 定义显示字段的名称 和 子属性的字段名称
      page:{
         // 放置页码及相关数据
         page: 1, //当前页码数
         pagesize: 10, //当前页面需要的数据条数
         total: 0 //记录总数
      },
      formData:{
        name:'',
        companyAddress:'',
        mailbox:'', //邮箱
        remarks:'', //备注
      },
      showDialog: false,
      showPermDialog: false, //控制分配权限弹层的显示或者隐藏
      // 专门接收新增或者编辑的弹框表单
      roleForm:{
        name:'',
        description:''
      },
      roleId: null, //用来记录当前分配权限的id
      rules:{
         name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
   async getRoleList(){
      const {total,rows} = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
   },
   changePage(newPage){
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getRoleList()
   },
   async getCompanyInfo(){
    this.formData = await getCompanyInfo(this.$store.getters.companyId)
   },
   async deleteRole(id){
     try {
        await this.$confirm('确认删除该角色吗')
        await deleteRole(id)
        this.getRoleList()
        this.$message.success('删除角色成功')
     } catch (error) {
        console.log(error);
     }
   },
   async editRole(id){
    this.roleForm = await getRoleDetail(id)
    this.showDialog = true // 为了不出现闪烁的问题 先获取数据 再弹出层
   },
   async btnOK(){
      try {
       // 只有校验通过的情况下 才会执行await的下方内容
       // roleForm这个对象有id就是编辑 没有id就是新增
        await this.$refs.roleForm.validate()
        if(this.roleForm.id){//编辑
         await updateRole(this.roleForm)
        }else{//新增
          // console.log(1233);
          await addRole(this.roleForm)
        }
        // 重新拉取数据
        this.getRoleList()
        this.$message.success('操作成功')
        this.showDialog = false // 关闭弹层  =>  触发el-dialog的事件close事件
      } catch (error) {
        console.log(error);
      }

   },
   btnCancel(){
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.roleForm.resetFields()
      this.showDialog = false
   },
  //  弹出层
  // 获取权限点数据 点击的时候调用 获取权限点数据
  async assignPerm(id){
    const result = await getPermissionList() //所有权限点
    this.permData = transListToTreeData(result,'0') //转换成树状结构
    this.roleId = id
    // 读取这个id的权限点  
    const {permIds} = await getRoleDetail(id) //permIds是当前角色所拥有的权限点数据
    this.selectCheck = permIds //当前角色所拥有的权限id付值
    this.showPermDialog = true
   },
  async btnPermOk(){
    // 调用el-tree的方法
     const permIds = this.$refs.permTree.getCheckedKeys()
     await assignPerm({permIds, id:this.roleId})
     this.$message.success('分配权限成功')
     this.showPermDialog = false
   },
   btnPermCancel(){
      // 重置数据
      this.selectCheck = []
      this.showPermDialog = false
   }
   
  }
}
</script>

<style>

</style>